const app = getApp()
import {
  getBanner,
  getDkList
} from "../../utils/request"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    baseUrl: app.baseUrl,
    swiperList: [],
    alert: false,
    list: [],
    loadMore: true,
    params: {
      keyword: '',
      pagesize: 10,
      pageint: 1,
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getBannerList()
    this.getList()
  },
  async getBannerList() {
    const data = await getBanner()
    if (data.code !== '1') return
    data.data.map(item => item.image_url = `${this.data.baseUrl}${item.image_url}`)
    console.log(data.data)
    this.setData({
      swiperList: data.data
    })
  },
  jumpImg(event) {
    const arr = []
    this.data.swiperList.map(item => arr.push(item.image_url))
    app.preview(event, arr)
  },
  //获取发布信息列表
  async getList() {
    const {
      params
    } = this.data
    const data = await getDkList(params)
    if (data.code !== '1') return
    this.data.params.pageint++
    data.data.data.map(item => {
      item.image_user = item.image_user.split("|")
      item.create_time = app.func.changeTime(item.create_time)
    })
    this.setData({
      list: [...this.data.list, ...data.data.data],
      params: {
        ...params
      },
    }, () => {
      this.setData({
        loadMore: this.data.list.length >= data.data.row_count ? false : true
      })
    })
  },
  setList(res) {
    const {
      params
    } = this.data
    params.pageint = 1
    this.setData({
      list: [],
      loadMore: true,
      params: {
        ...this.data.params,
        ...res
      }
    }, () => {
      this.getList()
    })
  },
  jumpSearch(event) {
    this.setList({
      keyword: event.detail.value
    })
    console.log(event)
  },
  getValue(event) {
    const {
      params
    } = this.data
    params.keyword = event.detail.value
    this.setData({
      params
    })
  },
  closeImg() {
    this.setData({
      alert: false
    })
  },
  openImg() {
    this.setData({
      alert: true
    })
  },
  jumpDetail(event) {
    wx.navigateTo({
      url: '/pages/clockDetail/index?id=' + event.currentTarget.dataset.id,
    })
  },
  async jumpClock() {
    const data = await app.checkUserInfo()
    if (data) {
      wx.navigateTo({
        url: '/pages/clocking/index',
      })
    } else {
      app.func.Toast.none("请完善个人信息")
      setTimeout(() => {
        wx.navigateTo({
          url: '/pages/person_info/index?type=1',
        })
      }, 1000)
    }
  },
  jumpClose() {
    const {
      params
    } = this.data
    params.keyword = ''
    this.setData({
      params
    })
  },
  goHome() {
    wx.navigateTo({
      url: '/pages/main/index',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.loadMore) {
      this.getList()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})